package com.africanmoviesmobile.webservice;

import java.io.IOException;
import java.util.Hashtable;

import org.json.JSONArray;
import org.json.JSONObject;
import org.pixmob.httpclient.HttpClientException;

import android.content.Context;

import com.africanmoviesmobile.webservice.inf.Playlist;
import com.africanmoviesmobile.webservice.inf.Request;
import com.africanmoviesmobile.webservice.inf.User;
import com.africanmoviesmobile.webservice.inf.Wishlist;

public class AmmUser extends AmmWebService implements User {

	private JSONObject _jsonObject;
	private int _resultPageCount;
	
	public AmmUser(JSONObject jsonObject, Context context) {
		super(context);
		_jsonObject = jsonObject;
	}

	public int getId() {
		return _jsonObject.optInt("user_id");
	}

	public String getName() {
		return _jsonObject.optString("name");
	}

	public String getEmail() {
		return _jsonObject.optString("email");
	}

	public Wishlist[] getWishlist(Hashtable<String, String> params) throws HttpClientException, IOException, WebServiceException {

		Wishlist[] result = null;

		if (!params.containsKey("email")) {
			params.put("email", getEmail());
		}

		try {

			JSONObject jo = read("wishlist/get", params);

			if ((jo != null) && (jo.length() > 0)) {

				_resultPageCount = jo.optInt("pageCount");
				
				JSONArray ja = jo.optJSONArray("data");

				if ((ja != null) && (ja.length() > 0)) {

					result = new Wishlist[ja.length()];

					for (int i = 0; i < ja.length(); i++) {
						result[i] = new AmmWishlist(ja.optJSONObject(i), getContext());
					}
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}

	public void getWishlist(final Hashtable<String, String> params, final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {

					Wishlist[] result = getWishlist(params);

					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
	}

	public boolean addWishlist(int movieId) throws HttpClientException, IOException, WebServiceException {

		boolean result = false;

		Hashtable<String, String> data = new Hashtable<String, String>();
		data.put("movieId", Integer.toString(movieId));
		data.put("email", getEmail());

		try {

			JSONObject jo = write("wishlist/add", data);

			if ((jo != null) && (jo.length() > 0)) {
				if ((jo.has("message")) && (jo.optString("message").equalsIgnoreCase("success"))) {
					result = true;
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}

	public void addWishlist(final int movieId, final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {
					boolean[] result = new boolean[1];
					result[0] = addWishlist(movieId);
					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
	}
	
	public boolean deleteWishlist(int movieId) throws HttpClientException, IOException, WebServiceException {

		boolean result = false;

		Hashtable<String, String> data = new Hashtable<String, String>();
		data.put("movieId", Integer.toString(movieId));
		data.put("email", getEmail());

		try {

			JSONObject jo = write("wishlist/delete", data);

			if ((jo != null) && (jo.length() > 0)) {
				if ((jo.has("message")) && (jo.optString("message").equalsIgnoreCase("success"))) {
					result = true;
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}
	
	public void deleteWishlist(final int movieId, final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {
					boolean[] result = new boolean[1];
					result[0] = deleteWishlist(movieId);
					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
	}
	
	public Playlist[] getPlaylist(Hashtable<String, String> params) throws HttpClientException, IOException, WebServiceException {

		Playlist[] result = null;

		if (!params.containsKey("email")) {
			params.put("email", getEmail());
		}

		try {

			JSONObject jo = read("playlist/get", params);

			if ((jo != null) && (jo.length() > 0)) {

				_resultPageCount = jo.optInt("pageCount");
				
				JSONArray ja = jo.optJSONArray("data");

				if ((ja != null) && (ja.length() > 0)) {

					result = new Playlist[ja.length()];

					for (int i = 0; i < ja.length(); i++) {
						result[i] = new AmmPlaylist(ja.optJSONObject(i), getContext());
					}
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}

	public void getPlaylist(final Hashtable<String, String> params, final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {

					Playlist[] result = getPlaylist(params);

					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
	}
	
	public boolean addPlaylist(int movieId) throws HttpClientException, IOException, WebServiceException {

		boolean result = false;

		Hashtable<String, String> data = new Hashtable<String, String>();
		data.put("movieId", Integer.toString(movieId));
		data.put("email", getEmail());

		try {

			JSONObject jo = write("playlist/add", data);

			if ((jo != null) && (jo.length() > 0)) {
				if ((jo.has("message")) && (jo.optString("message").equalsIgnoreCase("success"))) {
					result = true;
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}

	public void addPlaylist(final int movieId, final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {
					boolean[] result = new boolean[1];
					result[0] = addPlaylist(movieId);
					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
	}
	
	public Request[] getRequests(Hashtable<String, String> params) throws HttpClientException, IOException, WebServiceException {

		Request[] result = null;

		if (!params.containsKey("email")) {
			params.put("email", getEmail());
		}

		try {

			JSONObject jo = read("request/get", params);

			if ((jo != null) && (jo.length() > 0)) {
				
				_resultPageCount = jo.optInt("pageCount");

				JSONArray ja = jo.optJSONArray("data");

				if ((ja != null) && (ja.length() > 0)) {

					result = new Request[ja.length()];

					for (int i = 0; i < ja.length(); i++) {
						result[i] = new AmmRequest(ja.optJSONObject(i), getContext());
					}
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}

	public void getRequests(final Hashtable<String, String> params, final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {

					Request[] result = getRequests(params);

					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
	}

	public boolean addRequest(Hashtable<String, String> data) throws HttpClientException, IOException, WebServiceException {

		boolean result = false;

		if (!data.containsKey("email")) {
			data.put("email", getEmail());
		}

		try {

			JSONObject jo = write("request/add", data);

			if ((jo != null) && (jo.length() > 0)) {
				if ((jo.has("message")) && (jo.optString("message").equalsIgnoreCase("success"))) {
					result = true;
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}

	public void addRequest(final Hashtable<String, String> data, final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {
					boolean[] result = new boolean[1];
					result[0] = addRequest(data);
					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
	}
	
	public boolean deleteRequest(int requestId) throws HttpClientException, IOException, WebServiceException {

		boolean result = false;

		Hashtable<String, String> data = new Hashtable<String, String>();
		data.put("requestId", Integer.toString(requestId));
		data.put("email", getEmail());

		try {

			JSONObject jo = write("request/delete", data);

			if ((jo != null) && (jo.length() > 0)) {
				if ((jo.has("message")) && (jo.optString("message").equalsIgnoreCase("success"))) {
					result = true;
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}
	
	public void deleteRequest(final int requestId, final AsyncCallback listener, final Object state) {

		new Thread() {

			public void run() {

				try {
					boolean[] result = new boolean[1];
					result[0] = deleteRequest(requestId);
					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
	}

	public boolean updateInfo(Hashtable<String, String> data) throws HttpClientException, IOException, WebServiceException {

		boolean result = false;

		if (!data.containsKey("email")) {
			data.put("email", getEmail());
		}

		try {

			JSONObject jo = write("user/update", data);

			if ((jo != null) && (jo.length() > 0)) {
				if ((jo.has("message"))
						&& (jo.optString("message").equalsIgnoreCase("success"))) {
					result = true;
				}
			}

		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}

		return result;
	}

	public void updateInfo(final Hashtable<String, String> data, final AsyncCallback listener, final Object state) {

		new Thread() {
			public void run() {

				try {
					boolean[] result = new boolean[1];
					result[0] = updateInfo(data);
					try {
						listener.onComplete(result, state);
					} catch (Throwable t) {
						t.printStackTrace();
					}
				} catch (HttpClientException e) {
					listener.onHttpClientException(e, state);
				} catch (IOException e) {
					listener.onIOException(e, state);
				} catch (WebServiceException e) {
					listener.onWebServiceException(e, state);
				} catch (Throwable t) {
					t.printStackTrace();
				}
			}
		}.start();
	}
	
	public int getResultPageCount(){
		return _resultPageCount;
	}
	
	public boolean movieInWishlist(int movieId) throws HttpClientException, IOException, WebServiceException {
		
		boolean inWishlist = false;
		
		Wishlist[] userWishlist = null;
		
		try {
			userWishlist = (Wishlist[]) getWishlist(new Hashtable<String, String>());
		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}
		
		if ((userWishlist != null)) {
		
			for (int i = 0; i < userWishlist.length; i++) {
				
				Wishlist wishlist = (Wishlist) (userWishlist[i]);	
				
				if(wishlist.getMovieId() == movieId){
					inWishlist = true;
				}
				
			}
		}
		
		return inWishlist;
	}

	public boolean movieInPlaylist(int movieId) throws HttpClientException, IOException, WebServiceException {
		
		boolean inPlaylist = false;
		
		Playlist[] userPlaylist = null;  
		
		try {
			userPlaylist = (Playlist[]) getPlaylist(new Hashtable<String, String>());
		} catch (HttpClientException e) {
			throw e;
		} catch (IOException e) {
			throw e;
		} catch (WebServiceException e) {
			throw e;
		} catch (Throwable t) {
			t.printStackTrace();
		}
		
		if ((userPlaylist != null)) {
		
			for (int i = 0; i < userPlaylist.length; i++) {
				
				Playlist playlist = (Playlist) (userPlaylist[i]);	
				
				if(playlist.getMovieId() == movieId){
					inPlaylist = true;
				}
				
			}
		}
	
		return inPlaylist;
	}
	
}
